home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Network / PowerScript 14 / PowerDown RA < prev    next >
Encoding:
Text File  |  1999-03-04  |  10.8 KB  |  410 lines  |  [TEXT/ToyS]

  1. -- User setable properties
  2. property kasAllowAlerts : true -- Set to false to stop any alerts from appearing
  3. property kasServerAlerts : true -- Set to false to stop warnings about missed servers
  4. property kasFinder : "Finder" -- name of application that can shutdown and restart
  5.  
  6. -- Internal/User setable globals
  7. property gasLinkSet : false -- Has the friend been asked for? - set to true to never ask
  8. property gasLinkNeed : false
  9. property gasLinkUser : "CasaVision" -- Our friend on all machines
  10. property gasLinkPass : "" -- Our friend's password on all machines
  11.  
  12. -- Internal globals
  13. property gasOurZone : "" -- Set to zone of gasOurAlias
  14. property gasOurServer : "" -- Set to server of gasOurAlias
  15. property gasOurName : "" -- Name of application on remote server
  16. property gasOurAction : "" -- Set to choice of user (quit, shutdown, restart)
  17.  
  18. -- Remote Access Stuff
  19. property kraConnected : "connected"
  20. property kraProtocol : "ARAP"
  21. property gasRemote : false -- set internally to true if volume was a RemoteAccess volume
  22. property gasRemoteCfg : "" -- Set internally to ARA config needed for this access
  23. property gasRemoteModem : "" -- Set internally to modem for RA connection
  24. property gasRemoteServer : "" -- Set internally to check if we are already connected to the correct server
  25. property gasRemoteAlias : false --Set internally to rebuild ARA with alias instead of osax
  26. property gasRemoteNum : "" -- Set internally to the telephone number of the remote machine
  27. property gasRemoteUsr : "" -- Set internally to the login user for the remote machine
  28.  
  29.  
  30. on run
  31.     if (gasOurAction is "") then
  32.         ShowInitAlert()
  33.     else if (gasOurName is not "") then
  34.         RemoteAction(gasOurName, gasOurServer, gasOurZone, gasOurAction)
  35.     else
  36.         PutAwayVol(gasOurVol)
  37.     end if
  38. end run
  39.  
  40.  
  41. on open fsObjs
  42.     if option key down of (input state) then
  43.         if the number of items in fsObjs is not 1 then
  44.             ShowInitAlert()
  45.         else if (ShowChoice("You have the option key pressed." & return & return & ¬
  46.             "Do you wish to reinitialize this script with the object you dropped on it?")) then
  47.             SetScriptAlias(item 1 of fsObjs)
  48.         end if
  49.     else if (the number of items of fsObjs is not 1) then
  50.         ShowOpenAlert()
  51.     else
  52.         SetScriptAlias(item 1 of fsObjs)
  53.     end if
  54. end open
  55.  
  56.  
  57. on RemoteAction(appName, serverName, zoneName, whatToDo)
  58.     -- Get the user/password
  59.     if not GetFriend(false) then return
  60.     
  61.     if (serverName is not "") then
  62.         talk as user gasLinkUser ¬
  63.             with password gasLinkPass ¬
  64.             on server serverName ¬
  65.             in AppleTalk zone zoneName
  66.         
  67.         -- Mount the remote volume?
  68.         if (gasRemoteAlias) then
  69.             try
  70.                 «event ÅkuNResW» gasOurAlias ¬
  71.                     given «class USER»:gasLinkUser, «class PASS»:gasLinkPass
  72.             on error
  73.                 beep
  74.             end try
  75.         end if
  76.     end if
  77.     
  78.     if serverName is "" then
  79.         set suspect to application appName
  80.     else
  81.         set suspect to application appName ¬
  82.             of machine serverName ¬
  83.             of zone zoneName
  84.     end if
  85.     
  86.     try
  87.         -- Don't wait for a response, assume the aliases are correct?
  88.         if (whatToDo is "Shutdown") then
  89.             tell suspect to «event fndrshut»
  90.         else if (whatToDo is "Restart") then
  91.             tell suspect to «event fndrrest»
  92.         else if (whatToDo is "Quit") then
  93.             tell suspect to quit
  94.         else
  95.             beep 2
  96.         end if
  97.     on error
  98.         if (kasServerAlerts) then
  99.             ShowConnectAlert(appName)
  100.         else
  101.             beep
  102.         end if
  103.     end try
  104. end RemoteAction
  105.  
  106.  
  107. on ChooseScriptAlias()
  108.     if ShowChoices("Do you wish to activate this script with a folder/volume or a file?", {"Folder", "File"}) is "File" then
  109.         set myObj to ¬
  110.             choose file with prompt "Choose a server file for this PowerScript RA"
  111.     else
  112.         set myObj to ¬
  113.             choose folder with prompt "Choose a server folder or volume for this PowerScript RA"
  114.     end if
  115.     
  116.     SetScriptAlias(myObj)
  117. end ChooseScriptAlias
  118.  
  119.  
  120. on SetScriptAlias(aliasObj)
  121.     -- My Path
  122.     set myPath to (path to me)
  123.     
  124.     -- Grab an alias
  125.     set aInfo to alias info from aliasObj
  126.     set fInfo to basic info for aliasObj
  127.     
  128.     -- Save the info we need
  129.     set gasOurServer to alias server of aInfo
  130.     set gasOurZone to alias zone of aInfo
  131.     set gasOurVol to alias volume of aInfo
  132.     set gasOurName to original name of aInfo
  133.     
  134.     -- Remote info
  135.     set gasRemoteNum to ARA number of aInfo
  136.     set gasRemoteUsr to ARA user of aInfo
  137.     set gasRemotePwd to ARA password of aInfo
  138.     
  139.     -- Set our name to dropped name
  140.     set gasLinkNeed to true
  141.     set gasOurAction to "Quit"
  142.     set newName to (catalog name of fInfo)
  143.     
  144.     if (catalog kind of fInfo) is a folder then
  145.         set gasOurAction to "Put Away"
  146.         set gasLinkNeed to false
  147.         set newName to gasOurVol
  148.     else if (gasOurName is kasFinder) then
  149.         set choice to display dialog ¬
  150.             ("What do you wish to have the " & kasFinder ¬
  151.                 & " on " & gasOurServer & ¬
  152.                 " do when this script is run?") buttons ¬
  153.             {"Quit", "Restart", "Shutdown"} default button 3
  154.         set gasOurAction to button returned of choice
  155.         if (gasOurAction is not "Quit") then set newName to gasOurServer
  156.     end if
  157.     
  158.     -- Get the user/password
  159.     GetFriend(false)
  160.     
  161.     -- Check ARA connection
  162.     set gasRemote to IsRemoteAccessed()
  163.     if (gasRemote) then SaveRemoteAccess()
  164.     
  165.     -- Set our icon to dropped icon
  166.     set ourIcon to (the icon for aliasObj)
  167.     set the icon of myPath to ourIcon
  168.     
  169.     -- Refresh our new status in the finder
  170.     try
  171.         tell application "Finder" to update myPath -- only works with OS 8
  172.     on error
  173.         beep
  174.     end try
  175.     
  176.     -- Set our new name
  177.     set newName to "≈ " & gasOurAction & " " & newName
  178.     if (length of newName > 31) then set newName to the text from character 1 to 31 of newName
  179.     
  180.     try
  181.         collate myPath renaming it to newName
  182.     on error
  183.         beep
  184.     end try
  185. end SetScriptAlias
  186.  
  187.  
  188. on GetFriend(override)
  189.     if (gasOurServer is "") then -- Local?
  190.         set gasLinkSet to true
  191.         set gasLinkUser to kasLinkUserDft
  192.         set gasLinkPass to kasLinkPassDft
  193.     else if (gasLinkNeed) then
  194.         -- Build ARA connection?
  195.         if (gasRemote and not gasRemoteAlias) then
  196.             if not BuildRemoteAccess() then return false
  197.         end if
  198.         -- Get linking friend    
  199.         GetOneFriend(override)
  200.     end if
  201.     return true
  202. end GetFriend
  203.  
  204.  
  205. on GetOneFriend(override) -- LINKING ONLY VERSION!
  206.     set isLink to true -- Only linking in this script!
  207.     set userMode to "linking"
  208.     set defUser to gasLinkUser
  209.     set defPass to gasLinkPass
  210.     set passButtons to {"Cancel", "OK"}
  211.     set passButton to 2
  212.     
  213.     set usrPwd to KeyChainLookUp(gasOurZone, gasOurServer, isLink)
  214.     
  215.     if (override or usrPwd is {}) then
  216.         set chosen to display dialog ¬
  217.             "Enter the friendly " & userMode & ¬
  218.             " user's name…" default answer defUser ¬
  219.             default button 2 with icon note
  220.         
  221.         if (the button returned of chosen is "OK") then
  222.             set defUser to the text returned of chosen
  223.         else
  224.             return
  225.         end if
  226.         
  227.         set chosen to display dialog ¬
  228.             "Enter the friendly " & userMode & ¬
  229.             " user's password…" buttons passButtons ¬
  230.             default answer defPass default button passButton with icon note
  231.         
  232.         if (the button returned of chosen is not "Cancel") then
  233.             set defPass to the text returned of chosen
  234.         else
  235.             return
  236.         end if
  237.         
  238.         -- Save encrypted user/pass for future access
  239.         KeyChainSave(gasOurZone, gasOurServer, isLink, defUser, defPass, "")
  240.     else
  241.         set defUser to item 1 of usrPwd
  242.         set defPass to item 2 of usrPwd
  243.     end if
  244.     
  245.     set gasLinkSet to true
  246.     set gasLinkUser to defUser
  247.     set gasLinkPass to defPass
  248. end GetOneFriend
  249.  
  250.  
  251. on PutAwayVol(volName)
  252.     -- Volume
  253.     try
  254.         tell application "Finder"
  255.             update item named volName
  256.             pause for 2 with seconds timing -- Give some time to the Finder?
  257.             put away item named volName
  258.         end tell
  259.     on error
  260.         beep
  261.     end try
  262. end PutAwayVol
  263.  
  264.  
  265. on ShowAccessAlert()
  266.     ShowServerAlert("The remote access connection for the item on " & gasOurServer ¬
  267.         & " in zone " & gasOurZone & " failed to be made.")
  268. end ShowAccessAlert
  269.  
  270.  
  271. on ShowConnectAlert(appName)
  272.     ShowServerAlert("Couldn't connect to " & appName & " on " & ¬
  273.         gasOurServer & " in zone " & gasOurZone & return & return & ¬
  274.         "The server may be down or the app may not allow remote events.")
  275. end ShowConnectAlert
  276.  
  277.  
  278. on ShowServerAlert(msg)
  279.     set choice to ¬
  280.         display dialog msg buttons {"Reenter Password", "OK"} ¬
  281.             default button 2 with icon stop
  282.     
  283.     if (button returned of choice is not "OK") then GetFriend(true)
  284. end ShowServerAlert
  285.  
  286.  
  287. on ShowInitAlert()
  288.     if ShowChoices("This script must first be activated by dropping exactly one item on it." & ¬
  289.         return & return & "Please see the information file that accompanied it.", ¬
  290.         {"Activate", "OK"}) is not "OK" then ¬
  291.         ChooseScriptAlias()
  292. end ShowInitAlert
  293.  
  294.  
  295. on ShowOpenAlert()
  296.     ShowAlert("To reconfigure drop exactly one remote item on it.")
  297. end ShowOpenAlert
  298.  
  299.  
  300. on ShowAlert(msgStr)
  301.     if (kasAllowAlerts) then ¬
  302.         display dialog msgStr buttons {"Damn!"} ¬
  303.             default button 1 with icon stop
  304. end ShowAlert
  305.  
  306.  
  307. on ShowChoices(msgStr, choices)
  308.     set choice to ¬
  309.         display dialog msgStr buttons choices ¬
  310.             default button (number of items of choices) ¬
  311.             with icon stop
  312.     return (button returned of choice)
  313. end ShowChoices
  314.  
  315.  
  316. property kasKeyChainPassword : "PowerScript" -- Encrypt stored data with this
  317. property kasPrefsFileName : "PowerScript Prefs" -- File name in <Preferences>
  318.  
  319. on KeyChainLookUp(zoneName, serverName, isLinking)
  320.     set prefType to "πSRV"
  321.     if (isLinking) then set prefType to "πLNK"
  322.     
  323.     try
  324.         set myKeyData to load preference of type prefType ¬
  325.             named (zoneName & ":" & serverName) ¬
  326.             in file named kasPrefsFileName
  327.     on error
  328.         return {}
  329.     end try
  330.     
  331.     return (encrypt the data myKeyData ¬
  332.         with password kasKeyChainPassword)
  333. end KeyChainLookUp
  334.  
  335.  
  336. on KeyChainSave(zoneName, serverName, isLinking, usr, pwd, ntPwd)
  337.     set myKey to encrypt the data {usr, pwd, ntPwd} ¬
  338.         with password kasKeyChainPassword
  339.     
  340.     set prefType to "πSRV"
  341.     if (isLinking) then set prefType to "πLNK"
  342.     
  343.     save preference myKey of type prefType ¬
  344.         named (zoneName & ":" & serverName) ¬
  345.         in file named kasPrefsFileName
  346. end KeyChainSave
  347.  
  348.  
  349. on IsRemoteAccessed()
  350.     try
  351.         set araStats to (RA status)
  352.     on error
  353.         return false
  354.     end try
  355.     
  356.     return ((state of araStats) is kraConnected) ¬
  357.         and ((protocol of araStats) is kraProtocol)
  358. end IsRemoteAccessed
  359.  
  360.  
  361. on SaveRemoteAccess()
  362.     set gasRemoteAlias to true
  363.     
  364.     -- Get the ARA config for the alias
  365.     set configs to RA configurations whose numbers are gasRemoteNum ¬
  366.         whose users are gasRemoteUsr
  367.     
  368.     if (the number of items in configs) > 0 then
  369.         set gasRemoteCfg to item 1 of (item 1 of configs) -- The name of the config to use
  370.         set gasRemoteServer to server name of (RA status)
  371.         set gasRemoteModem to «event ÅkuNMdmÇ»
  372.         set gasRemoteAlias to false
  373.     end if
  374. end SaveRemoteAccess
  375.  
  376.  
  377. on BuildRemoteAccess()
  378.     set araIsOn to IsRemoteAccessed()
  379.     
  380.     if (araIsOn) then
  381.         -- Connected to correct server?
  382.         if (server name of (RA status)) is gasRemoteServer then
  383.             return true
  384.         else
  385.             RA disconnect
  386.             set araIsOn to IsRemoteAccessed()
  387.         end if
  388.     end if
  389.     
  390.     if (not araIsOn) then
  391.         -- Save current config, set it to ours, connect, set it back
  392.         set saveConfig to RA configuration gasRemoteCfg
  393.         set saveModem to «event ÅkuNMdmÇ» gasRemoteModem
  394.         
  395.         try
  396.             RA connect
  397.         on error
  398.             beep
  399.         end try
  400.         
  401.         RA configuration saveConfig
  402.         «event ÅkuNMdmÇ» saveModem
  403.         
  404.         if (IsRemoteAccessed()) then return true
  405.     end if
  406.     
  407.     ShowAccessAlert()
  408.     return false
  409. end BuildRemoteAccess
  410.